import 'package:flutter/material.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';
import 'package:kq_flutter_core_widget/widgets/image/kq_image.dart';
import 'package:kq_flutter_pad_widgets/config/kq_pad_global.dart';
import 'package:kq_flutter_pad_widgets/resources/kq_pad_theme_colors.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';

import '../../../resources/images.dart';

class KqPadRadioButton<T extends IRadioEntity> extends StatefulWidget {
  ///排列方向，默认水平排列
  ///
  ///水平方向[RadioDirection.horizontal]
  ///
  ///垂直方向[RadioDirection.vertical]
  final RadioDirection direction;

  /// 数据，需继承[IRadioEntity]
  final List<T>? radioEntity;

  /// 点击回调
  ///
  /// 回调的下标[index]
  final Future<bool> Function(int index)? onChanged;

  ///文本宽度，不传代表自适应，传则代表固定指定宽度
  final double? textFixWidth;

  ///默认选择的index。
  ///可配合[needUpdateDefaultIndex]字段让[defaultIndex]每次都生效。
  final int defaultIndex;

  ///是否需要改变[defaultIndex]。
  ///如果设置成ture，则每次传入的[defaultIndex]都会生效。
  final bool needUpdateDefaultIndex;

  const KqPadRadioButton({
    super.key,
    this.direction = RadioDirection.horizontal,
    this.radioEntity,
    this.onChanged,
    this.textFixWidth,
    this.defaultIndex = 0,
    this.needUpdateDefaultIndex = false,
  });

  @override
  State<StatefulWidget> createState() => _KqPadRadioButtonState<T>();
}

class _KqPadRadioButtonState<T extends IRadioEntity>
    extends State<KqPadRadioButton<T>> {
  int _selectedValue = 0;

  @override
  void initState() {
    _selectedValue = widget.defaultIndex;
    super.initState();
  }

  @override
  void didUpdateWidget(covariant KqPadRadioButton<T> oldWidget) {
    if (widget.needUpdateDefaultIndex) {
      _selectedValue = widget.defaultIndex;
    }
    super.didUpdateWidget(oldWidget);
  }

  @override
  Widget build(BuildContext context) {
    if (widget.direction == RadioDirection.horizontal) {
      return SingleChildScrollView(
        scrollDirection: Axis.horizontal,
        child: Row(
          children: getList(context),
        ),
      );
    } else {
      return SingleChildScrollView(
        child: Column(
          children: getList(context),
        ),
      );
    }
  }

  List<Widget> getList(BuildContext context) {
    List<Widget> list = [];
    if (widget.radioEntity != null && widget.radioEntity!.isNotEmpty) {
      for (int i = 0; i < widget.radioEntity!.length; i++) {
        T t = widget.radioEntity![i];
        list.add(
          RadioItemTile(
            title: SizedBox(
              width: widget.textFixWidth,
              child: Text(
                t.title ?? "",
                style: TextStyle(
                    fontSize: 8.r,
                    color: KqPadThemeColors.text26,
                    overflow: TextOverflow.ellipsis),
              ),
            ),
            value: i,
            groupValue: _selectedValue,
            onChanged: (value) async {
              if (widget.onChanged != null) {
                if (await widget.onChanged!.call(value)) {
                  _selectedValue = value;
                  setState(() {});
                }
              } else {
                _selectedValue = value;
                setState(() {});
              }
            },
          ),
        );
      }
    }
    return list;
  }
}

enum RadioDirection { vertical, horizontal }

class IRadioEntity {
  final String? title;

  IRadioEntity(this.title);
}

class RadioItemTile extends StatelessWidget {
  final Widget? title;
  final int value;
  final int groupValue;
  final String? checkImage;
  final String? uncheckImage;
  final Function(int value)? onChanged;

  const RadioItemTile({
    super.key,
    this.title,
    required this.value,
    required this.groupValue,
    this.checkImage,
    this.uncheckImage,
    this.onChanged,
  });

  @override
  Widget build(BuildContext context) {
    return KqInkWell(
      enableRipple: false,
      child: Row(
        children: [
          KqImage.assets(
            url: value == groupValue
                ? checkImage ?? Images.commonIcRadio16SelectedEnabled
                : uncheckImage ?? Images.commonIcRadio16UnselectedEnabled,
            package: KqPadGlobal.packageName,
            width: 8.r,
            height: 8.r,
          ),
          SizedBox(width: 4.r),
          title ?? Container(),
          SizedBox(width: 4.r),
        ],
      ),
      onTap: () {
        onChanged?.call(value);
      },
    );
  }
}
